package com.dc.web;

import lombok.Data;
import com.dc.service.BlogService;
import com.dc.service.CommentService;
import com.dc.service.TagService;
import com.dc.service.TypeService;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.data.domain.Pageable;
import org.springframework.data.domain.Sort;
import org.springframework.data.web.PageableDefault;
import org.springframework.stereotype.Controller;
import org.springframework.ui.Model;
import org.springframework.web.bind.annotation.GetMapping;
import org.springframework.web.bind.annotation.PathVariable;
import org.springframework.web.bind.annotation.RequestParam;
import org.springframework.web.bind.annotation.ResponseBody;

@Controller
public class IndexController {

    @Autowired
    private BlogService blogService;
    @Autowired
    private TypeService typeService;
    @Autowired
    private TagService tagService;
    @Autowired
    private CommentService commentService;

    @GetMapping("/")
    public String index(@PageableDefault(size = 7, sort = {"updateTime"}, direction = Sort.Direction.DESC)
                                    Pageable pageable, Model model){
        model.addAttribute("page",blogService.listBlog(pageable));
        model.addAttribute("types",typeService.listTypeTop(6));
        model.addAttribute("tags",tagService.listTagTop(10));
        model.addAttribute("comments",commentService.listCommentTop(4));
        model.addAttribute("recommendBlogs",blogService.listRecommendBlogTop(8));
        return "index";
    }

    @GetMapping("/search")
    public String search(@PageableDefault(size = 6, sort = {"updateTime"}, direction = Sort.Direction.DESC)
                                 Pageable pageable,
                         @RequestParam String query,
                         Model model){
        model.addAttribute("page",blogService.listBlog("%"+query+"%",pageable));
        model.addAttribute("query",query);
        return "search";
    }


    @GetMapping("/blog/{id}")
    public String blog(@PathVariable Long id,
                       Model model) {
        model.addAttribute("blog", blogService.getAndConvertBlog(id));
        return "blog";
    }



    @GetMapping("/footer/newblog")
    public String newblogs(Model model) {
        model.addAttribute("newblogs", blogService.listRecommendBlogTop(3));
        return "_fragments :: newblogList";
    }





    @GetMapping("/json")
    @ResponseBody
    public Dog json(){
        Dog dog = new Dog();
        dog.setId(10001231l);
        dog.setAge(5);
        dog.setName("大白");
        return dog;
    }

    @GetMapping("json1")
    @ResponseBody
    public String json1(){
        return "{\"resultcode\":\"200\",\"reason\":\"查询成功\",\"result\":{\"Country\":\"中国\",\"Province\":\"云南\",\"City\":\"昆明\",\"District\":\"\",\"Isp\":\"电信\"},\"error_code\":0}";
    }


}

@Data
class Dog{
    private Long id;
    private String name;
    private Integer age;
}

/*       int i =9/0;//500
        String blog = null;
        if(blog==null){
            //throw new RuntimeException("博客找不到");
            throw new NotFoundException("博客找不到");
        }*/

